import peewee

from glow_serializer import Serializer, NestedField

database = peewee.MySQLDatabase(
    # 数据库名称需要根据实际情况填写
    # 'xxxxx',
    host='localhost',
    user='root',
    password='123456',
    port=3306,
)


class User(peewee.Model):
    username = peewee.CharField()
    nickname = peewee.CharField()

    class Meta:
        database = database


class Book(peewee.Model):
    name = peewee.CharField()
    price = peewee.IntegerField()
    user = peewee.ForeignKeyField(User, backref='books')

    class Meta:
        database = database


class UserSerializer(Serializer):
    class Meta:
        fields = ['id', 'username', 'nickname']


class BookSerializer(Serializer):
    user = NestedField(UserSerializer(exclude=['id']))

    class Meta:
        fields = ['id', 'name', 'price', 'user']


class ReverseBookSerializer(Serializer):
    class Meta:
        fields = ['id', 'name', 'price']


class ReverseUserSerializer(Serializer):
    books = NestedField(ReverseBookSerializer(many=True))

    class Meta:
        fields = ['id', 'username', 'nickname', 'books']


if __name__ == '__main__':
    # 建表
    # database.connect()
    # database.create_tables([User, Book])

    # books为需要序列化的orm对象集，此处以peewee为例，兼容大部分主流orm
    books = Book.select().where(Book.id != 0)
    data1 = BookSerializer().dumps(books, many=True)
    print(data1)
    # 下面的示例为反向查询并序列化，即从user查books
    users = User.select().where(User.id != 0)
    data2 = ReverseUserSerializer().dumps(users, many=True)
    print(data2)
